//
//  ArchiverUnarchiver.m
//  Gekko
//
//  Created by jose on 08/06/13.
//  Copyright (c) 2013 Jose Luis Lopez. All rights reserved.
//

#import "ArchiverUnarchiver.h"

@implementation ArchiverUnarchiver

+(void)archiveReceipt:(Receipt *)receipt {
    
    // Build the path to the data file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *pathUser = [documentsPath stringByAppendingPathComponent:[[NSUserDefaults standardUserDefaults] valueForKey:kKeyUserId]];
    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:pathUser])
        [[NSFileManager defaultManager] createDirectoryAtPath:pathUser withIntermediateDirectories:YES attributes:nil error:&error];

    if ([[NSFileManager defaultManager] fileExistsAtPath:pathUser]) {

        NSLog(@"User folder is pressent");
    } else {
        
        NSLog(@"User folder NOT present, %@", error);
    }

    NSString *pathReceipt = [pathUser stringByAppendingPathComponent:receipt.idi];
    
    NSLog(@"The path is %@", pathReceipt);
    

    if ([[NSFileManager defaultManager] fileExistsAtPath:pathReceipt]) {
        
        NSLog(@"Receipt founded");
        [[NSFileManager defaultManager] removeItemAtPath:pathReceipt error:nil];
    } else {

        NSLog(@"Receipt NOT founded");
    }
    
        
    if ([NSKeyedArchiver archiveRootObject:receipt toFile:pathReceipt]) {
        NSLog(@"No error saving");
    } else
        NSLog(@"Error saving");
}

+ (NSMutableArray *)unarchiveReceipts {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *pathUser = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@/",[[NSUserDefaults standardUserDefaults] valueForKey:kKeyUserId]]];
    NSLog(@"The path is %@", pathUser);
    
    NSMutableArray *arrayReceipts = [[NSMutableArray alloc] init];
    NSArray *arrayFiles;
    if ([[NSFileManager defaultManager] fileExistsAtPath:pathUser]) {
        NSLog(@"The file exists");
        NSError *error = nil;
        arrayFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathUser error:&error];
        NSLog(@"The error is %@", error);
        NSLog(@"The arrayfiles is %@", arrayFiles);
    } else {
        NSLog(@"The file doesnt exist");
        arrayFiles = [[NSMutableArray alloc] init];
    }
    
    for (NSString *string in arrayFiles) {

        NSLog(@"Filepath %@", [NSString stringWithFormat:@"%@/%@", pathUser, string]);
        if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", pathUser, string]]) {
            NSLog(@"The file exist");
        } else
            NSLog(@"The file doesnt exist");
        NSObject *receipt = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString stringWithFormat:@"%@/%@", pathUser, string]];
        if (receipt)
            [arrayReceipts addObject:receipt];
        
    }
    NSLog(@"The array is %@", arrayReceipts);
    NSLog(@"++++++++++++++");
    return arrayReceipts;
}

+ (void)deleteReceipt:(Receipt *)receipt {
    
    // Build the path to the data file
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *pathUser = [documentsPath stringByAppendingPathComponent:[[NSUserDefaults standardUserDefaults] valueForKey:kKeyUserId]];
    NSError *error = nil;
    if (![[NSFileManager defaultManager] fileExistsAtPath:pathUser])
        [[NSFileManager defaultManager] createDirectoryAtPath:pathUser withIntermediateDirectories:YES attributes:nil error:&error];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:pathUser]) {
        
        NSLog(@"User folder is pressent");
    } else {
        
        NSLog(@"User folder NOT present, %@", error);
    }
    
    NSString *pathReceipt = [pathUser stringByAppendingPathComponent:receipt.idi];
    
    NSLog(@"The path is %@", pathReceipt);
    
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:pathReceipt]) {
        
        NSLog(@"Receipt founded");
        [[NSFileManager defaultManager] removeItemAtPath:pathReceipt error:nil];
    } else {
        
        NSLog(@"Receipt NOT founded");
    }
}

+ (void)clearData {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *pathUser = [documentsPath stringByAppendingPathComponent:[[NSUserDefaults standardUserDefaults] valueForKey:kKeyUserId]];

    if ([[NSFileManager defaultManager] fileExistsAtPath:pathUser]) {
        
        NSLog(@"Exist");
        if ([[NSFileManager defaultManager] removeItemAtPath:pathUser error:nil])
            NSLog(@"Removed");
        else
            NSLog(@"Not remove");
    }
}

@end
